package task.manager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

import android.os.Environment;
import android.util.Log;

/**
 * 
 * @author Harrison Saylor, Aaron Mead, Marc Huet, Greg Ruskin
 * @version 1.0
 */
public class TaskFacade {
	
	private TaskList taskList;
	private CategoryFacade categoryFacade;
	private static TaskFacade INSTANCE;
	
	private String activeTask;
	
	/**
	 * Returns an instance.
	 * 
	 * @return An instance.
	 */
	public static TaskFacade getInstance() {
		
		if(INSTANCE == null) {
			INSTANCE = new TaskFacade();
		} // End of if statement.
		
		return INSTANCE;
		
	} // End of getInstance method.
	
	/**
	 * TaskFacade default constructor.
	 */
	public TaskFacade() {
		
		categoryFacade = CategoryFacade.getInstance();
		
		try { // Try to load task datastructure from a file.
			loadTasks();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // End of try/catch statements.
		
		System.out.println("taskList is" + taskList);
		
		if(taskList == null){
			//List<Task> tempList = new ArrayList<Task>();
			taskList = new TaskList();
		} // End of if statement.
		System.out.println("2 taskList is" + taskList);
	} // End of TaskFacade constructor.
	
	/**
	 * Adds a task to the task list.
	 * 
	 * @param task Task to add to the task list.
	 */
	public void addTask(Task task) {
		System.out.println(taskList);
		taskList.addTask(task);
	} // End of addTask method.
	
	/**
	 * Loads the tasks from a file.
	 * 
	 * @throws IOException Exception handling for storage file.
	 */
	public void loadTasks() throws IOException {
		ObjectInputStream ois = null;
		try {
			//we are using external storage, so we need to get the location
			//be sure you have the correct privileges 
			File file = new File(Environment.getExternalStorageDirectory() + "/" + categoryFacade.getActiveCategory() + "/tasks.bin");
			ois = new ObjectInputStream(new FileInputStream(file));
			taskList = (TaskList)ois.readObject();
		} catch (StreamCorruptedException e) {
			Log.e("APPLICATION FACADE", "Corrupted binary file");
		} catch (FileNotFoundException e) {
			Log.e("APPLICATION FACADE", "Binary File not found");
			System.out.println("Binary File not found");
		} catch (IOException e) {
			Log.e("APPLICATION FACADE", "General IO Error");
		} catch (ClassNotFoundException e) {
			Log.e("APPLICATION FACADE", "Class not found");
		} finally {
			if (ois != null)
				ois.close();
		} // End of try/catch/finally statements.
		
	} // End of loadUsers method.
	
	/**
	 * Saves the tasks to a file.
	 * 
	 * @throws IOException Exception handling for storage file.
	 */
	public void saveTasks() throws IOException {
		ObjectOutputStream oos = null;
		try {
			File file = new File(Environment.getExternalStorageDirectory() + "/" + categoryFacade.getActiveCategory() + "/tasks.bin");
			System.out.println("File created");
			oos = new ObjectOutputStream(new FileOutputStream(file));
			System.out.println("ObjectOutputStream instantiated");
			oos.writeObject(taskList);
			System.out.println("users object written to file");
			oos.flush();
			System.out.println("objectOutputStream flushed");
		} catch (IOException e) {
			Log.e("TaskManagerActivity", "Binary file input general error");
			e.printStackTrace();
		} finally {
			if (oos != null) {
				oos.close();
			} // End of if statement.
		} // End of try/catch/finally statements.
	} // End of saveUsers method.
	
	public TaskList getTaskList(){
		return taskList;
	}
	
	
	public void setTaskList(TaskList t){
		System.out.println("Change taskList to: " + t);
		taskList=t;
		
	}
	
	public void setActiveTask(String taskName){
		activeTask = taskName;
	}
	
	public String getActiveTask(){
		return activeTask;
	}

	public Task getTask(String taskName) {
		
		return taskList.getTask(taskName);
	}
	
} // End of TaskFacade class.
